#ifndef EXECUTE_H
#define EXECUTE_H


// service unit
typedef struct
{
    char*  binpath;
    char*  binname;
    char** agrv;
    char** env;
    char*  std_in;
    char*  std_out;
    char*  std_err;
    char*  conf_file;
    char*  conf_dir;
    char*  wantedby;
    int    enabled;
    int    start_type;
    int    restart_count;
    long   conf_lastmtime;
    void*  desc;
}Service_t;

// socket unit
typedef struct
{
    char*  service;
    char*  conf_file;
    char*  conf_dir;
    char*  sock_path;
    char*  stream_path;
    char*  stream_addr;
    char*  dgram_path;
    char*  dgram_addr;
    int    stream_unixfd;
    int    stream_fd;
    int    dgram_unixfd;  
    int    dgram_fd; 
    int    listening;
    long   conf_lastmtime;
}Socket_t;

// unit task:[service,socket...]
typedef struct
{
    Service_t     service;
    Socket_t*     sockets;
    unsigned int  socketslen;
    struct status
    {
        int         running;
        int         pid;
        int         ready;
        long        cpu_time;
        long        mem_usage;
    }status;  
}Task_t; 


typedef struct
{
    char*       name;
    char*       conf_file;
    char*       conf_dir;
    char*       wantedby;
    Task_t*     tasks;
    int         taskslen;
    void*       next;
    void*       last;
    void*       headptr;
    void*       tailptr;
    long        conf_lastmtime;    
    struct state
    {
        int current_ready;

    }status;
    
}Target_t;

// target ring in bootmanage.
typedef struct
{
    Target_t*       upper; //ring header
    Target_t*       lower; 
    Target_t*       now;
    int             ring_max;
    int             index;
    int             curr_num;
    int             flag;     //boot or stop
}bootm_t;


int execute(Task_t *task,void *pipe_handler);
int start(Target_t *target,void *hook);
int stopOrkill(int pid);
void terminate(Target_t *target);
int check_alive(Target_t * target);
int check_status(Target_t * target);
Task_t *lookup_task(Target_t *target,int pid,const char *service);

bootm_t *bootm_init(const char* dir,const char *__default__);

#endif